package com.hung.my.dao.impl;

import java.util.ArrayList;
import java.util.List;


import org.hibernate.HibernateException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hung.my.dao.UserDao;
import com.hung.my.model.User;


public class UserDaoImpl extends HibernateDaoSupport implements UserDao{

	@Override
	public boolean add(User user) {
		// TODO Auto-generated method stub
		boolean flag = false;
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().save(user);
			flag = true;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public boolean update(User user) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try {
			getHibernateTemplate().saveOrUpdate(user);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean delete(User entity) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public User find(User entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> fillAll() {
		// TODO Auto-generated method stub
		List<User> listUser = new ArrayList<User>();
		try {
			String queryString = "from User";
			listUser = getHibernateTemplate().find(queryString);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listUser;
	}

	@Override
	public List<User> findRange(int from, int to) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public User getById(int Id) {
		// TODO Auto-generated method stub
		User user = new User();
		
		List<User> listUser = new ArrayList<User>();
		try {
			String queryString = "from User";
			listUser = getHibernateTemplate().find(queryString);
			user = listUser.get(0);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public boolean checkLogin(User user) {
		// TODO Auto-generated method stub
		boolean flag = false;
		
		try {
			List<User> listUser = new ArrayList<User>();
			String sqlQuerryString =  "select * from User where Username='" + user.getUserName() + "'"
					+ "and UserPassword='" + user.getUserPassword() + "'";
			listUser = getSession().createSQLQuery(sqlQuerryString).list();
			if(listUser != null){
				if (listUser.size() > 0) {
					flag = true;
				}
			}
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			flag = false;
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			flag = false;
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}

}
